package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.data.GetClue;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.ClueMeesageEntity;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class CustomerAddContactMessageActivity extends AppCompatActivity {
    ImageView img_commt, img_back;
    EditText edit_contact_name, edit_respectful, edit_contact_address,
            edit_contact_phone,edit_contact_cphone,edit_contanct_qq,edit_contanct_weixin,edit_contanct_weibo ;
    ContactMessageEntity contact;
    String result;
    ClueMeesageEntity clue;
    public static CustomerAddContactMessageActivity a;
    String[] sex;
    CustomerMessageEntity customer;
    String UTag;
    private boolean tag = false;
    int Tag;
    private GoogleApiClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_add_message);
        a=this;
        img_back = (ImageView) findViewById(R.id.contact_add__img_back);
        img_commt = (ImageView) findViewById(R.id.contact_add_img_commit);
        edit_contact_cphone= (EditText) findViewById(R.id.contact_add_cphone);
        edit_contanct_qq = (EditText) findViewById(R.id.contact_add_edit_qq);
        edit_contanct_weixin = (EditText) findViewById(R.id.contact_add_edit_weixin);
        edit_contanct_weibo = (EditText) findViewById(R.id.contact_add_edit_webo);
        edit_contact_address = (EditText) findViewById(R.id.contact_add_edit_detailed_addresss);
        edit_contact_phone = (EditText) findViewById(R.id.contact_add_edit_mobile_phone);
        edit_contact_name = (EditText) findViewById(R.id.contact_add_edit_name);
        edit_respectful = (EditText) findViewById(R.id.contact_add_edit_respectful);
        img_commt.setImageResource(R.drawable.next);
        if ("ClueToCustomer".equals(getIntent().getStringExtra("ClueToCustomerTag"))){
            showMessage();
        }
        if ("ClueToCustomerGroup".equals(getIntent().getStringExtra("ClueToCustomerGroupTag"))){
            showMessage();
        }
        if ("wanshan".equals(getIntent().getStringExtra("wanshan"))) {
            contact = GetContact.get();
            customer = GetCustumer.get();
        }
        a = this;
        Intent intent = getIntent();
        result = intent.getStringExtra("spinner");
        img_commt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tag = getIntent().getIntExtra("Tag", 0);
                if (Tag == 0) {
                    setData();
                }
                Intent intent = new Intent(CustomerAddContactMessageActivity.this, CustomerAddMessageActivity.class);
                intent.putExtra("Tag", Tag);
                intent.putExtra("UTag", UTag);
                if ("updata".equals(getIntent().getStringExtra("updata"))){
                    intent.putExtra("updata","updata");
                }
                intent.putExtra("dockid",getIntent().getIntExtra("dockid",0));
                if ("ClueToCustomer".equals(getIntent().getStringExtra("ClueToCustomerTag"))){
                    intent.putExtra("ClueToCustomerTag","ClueToCustomer");
                }
                if ("ClueToCustomerGroup".equals(getIntent().getStringExtra("ClueToCustomerGroupTag"))){
                    intent.putExtra("ClueToCustomerGroupTag","ClueToCustomerGroup");
                }
                intent.putExtra("kehuchiTag",getIntent().getStringExtra("kehuchiTag"));
                startActivity(intent);

            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData();
                finish();
            }
        });


        if ("updata".equals(getIntent().getStringExtra("updata"))){
            setMessage();
        }

        if ("wanshan".equals(getIntent().getStringExtra("wanshan"))) {
            if (contact != null) {
                setinfo();
            }
        }
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
    public void showMessage(){
        clue= GetClue.get();
        edit_contact_name.setText(clue.getClue_contact());
        edit_contact_phone.setText(clue.getClue_mobile_phone());
        edit_contact_address.setText(clue.getClue_address());
    }
    public void setinfo(){
        edit_contact_name.setText(contact.getContact_name());
        edit_contact_phone.setText(contact.getFirst_contact_mobile_phone());
    }
    public void setMessage() {
        edit_contact_name.setText(contact.getContact_name());
        edit_respectful.setText(contact.getContact_respectful());
        edit_contact_phone.setText(contact.getFirst_contact_mobile_phone());
        edit_contact_address.setText(contact.getContact_address());
        edit_contact_cphone.setText(contact.getSend_contact_phone());
        edit_contanct_qq.setText(contact.getContact_qq());
//        Log.d("bb",contact.getContact_qq());
        edit_contanct_weixin.setText(contact.getContact_wechat());
        edit_contanct_weibo.setText(contact.getContact_weibo());
    }

    public void setData() {
        contact.setContact_mobile_phone(edit_contact_phone.getText().toString());
        contact.setContact_address(edit_contact_address.getText().toString());
        contact.setContact_name(edit_contact_name.getText().toString());
        contact.setContact_respectful(edit_respectful.getText().toString());
        contact.setSend_contact_phone(edit_contact_cphone.getText().toString());
        contact.setContact_qq(edit_contanct_qq.getText().toString());
        contact.setContact_wechat(edit_contanct_weixin.getText().toString());
        contact.setContact_weibo(edit_contanct_weibo.getText().toString());

    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("CustomerAddContactMessage Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}
